/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.BehavioralFeature;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Manages an Activity
 *
 * @author Gilles Cannenterre
 * @since 23 mars 07
 */
public class NodeActivityHelper extends NodeHelper {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getBaseString() {
		return "Activity_"; //$NON-NLS-1$
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Element createUMLElement(String name, Element umlParent){
		Activity anActivity = null;
		if (umlParent instanceof BehavioredClassifier) {
			anActivity = UMLFactory.eINSTANCE.createActivity();
			anActivity.setName(name) ;
			((BehavioredClassifier)umlParent).getOwnedBehaviors().add(anActivity);		
		}
		if (umlParent instanceof BehavioralFeature) {
			anActivity = UMLFactory.eINSTANCE.createActivity();
			anActivity.setName(name) ;
			((BehavioralFeature)umlParent).getMethods().add(anActivity);		
		}
		return anActivity;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent){
		if ( (umlParent instanceof BehavioredClassifier) 
				|| (umlParent instanceof BehavioralFeature) ) {
			return true;
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Dimension getGraphNodeSize() {
		return new Dimension(600,300);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof BehavioredClassifier) {
			return ((BehavioredClassifier)umlParent).getOwnedBehaviors().add((Behavior)element);
		}
		if(umlParent instanceof BehavioralFeature) {
			return ((BehavioralFeature)umlParent).getMethods().add((Behavior)element);
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getNewUMLElementName(Element umlParent) {
		String name = ""; //$NON-NLS-1$

		// i <10000: avoid infinite loops
		for(int i=0; i< 10001; i++ ) {
			name = getBaseString()+i;
			if (umlParent instanceof BehavioredClassifier){
				if(null == ((BehavioredClassifier)umlParent).getOwnedBehavior(name)) {
					return name;
				}
			}
			if (umlParent instanceof BehavioralFeature){
				if(null == ((BehavioralFeature)umlParent).getMethod(name)) {
					return name;
				}
			}
		}
		return getBaseString()+"X"; //$NON-NLS-1$
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if (umlParent instanceof BehavioredClassifier) {
			return ((BehavioredClassifier)umlParent).getOwnedBehaviors().remove(element);
		}
		if(umlParent instanceof BehavioralFeature) {
			return ((BehavioralFeature)umlParent).getMethods().remove(element);
		}
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if(parent instanceof Diagram) {
			return true;
		} else if (parent.getSemanticModel()!=null){
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt!=null){
				return canCreateUMLElement(parentElt);
			}
		}
		return false;
	}
}